<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <p>{{newMsg}}</p>
    <hr>
    <p v-for="(item, index) in list" :key="index">
      <span>名称：{{item.name}}</span>
      <span>单价：{{item.price}}</span>
      <span>数量：{{item.num}}</span>
      <span>小计：{{item.num * item.price}}</span>
    </p>
    <p>总价1：{{total1}}</p>
    <p>总价1：{{total1}}</p>
    <p>总价1：{{total1}}</p>
    <p>总价2：{{total2()}}</p>
    <p>总价2：{{total2()}}</p>
    <p>总价2：{{total2()}}</p>
  </div>
  <script src="../vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        msg: 'hello',
        list: [
          {name: 'apple', price: 5, num: 3},
          {name: 'banana', price: 4, num: 4},
          {name: 'peach', price: 3, num: 3},
        ]
      },
      // 计算属性：用来做计算的一个属性,可以认为是在data的基础上计算得出了一条数据，不会修改原始数据
      // 依赖缓存：当源数据没有变化时，无论调用多少次，都只执行一次。适用于性能开销大，反复计算的场景
      computed: {
        newMsg() {
          return this.msg.split('').reverse().join('')
        },
        total1() {
          console.log('computed');
          let num = 0;
          this.list.forEach(item => {
            num += item.price * item.num;
          });
          return '￥'+num.toFixed(2)+'元'
        }
      },
      methods: {
        total2() {
          console.log('methods');
          let num = 0;
          this.list.forEach(item => {
            num += item.price * item.num;
          });
          return '￥'+num.toFixed(2)+'元'
        }
      },
    })
  </script>
</body>
</html>